package com.biye.api.service.impl;

import com.biye.api.service.ExcelTemplateService;
import com.biye.api.tool.ExcelTemplate.PersonInfoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ExcelTemplateServiceImpl implements ExcelTemplateService {

    /**
     * @Author Maple.Chen
     * @Description 该方法弃用，使用下边的downloadPersonInfoTemplate
     * @Date 14:05 2021/3/16
     **/
    @Override
    public void downloadTemplate(HttpServletResponse response, String filename) {
        byte[] buff = new byte[1024];
        //创建缓冲输入流
        BufferedInputStream bis = null;
        OutputStream outputStream = null;

        try {
            outputStream = response.getOutputStream();

            //这个路径为待下载文件的路径
            bis = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\abc18\\Desktop" + File.separator + filename )));
            int read = bis.read(buff);

            //通过while循环写入到指定了的文件夹中
            while (read != -1) {
                outputStream.write(buff, 0, buff.length);
                outputStream.flush();
                read = bis.read(buff);
            }


        } catch ( IOException e ) {
            e.printStackTrace();
            //出现异常返回给页面失败的信息
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void downloadPersonInfoTemplate(HttpServletResponse response, String filename) {

        // 定义模板文件名称
        String template_name = "师生信息导入模板";
        // 教职工信息导入模板格式
        String sheet_name_1 = "Teacher";
        String title_1 = "教职工信息导入模板";
        List<String> list_1 = new ArrayList<>();
        list_1.add("tno"); //职工编号
        list_1.add("name"); //姓名
        list_1.add("passwd"); //初始密码
        list_1.add("sex"); //性别
        list_1.add("idcard"); //身份证号
        list_1.add("phone"); //手机号
        list_1.add("mail"); //邮箱
        list_1.add("dept"); //所属部门名称
        Map<String, String> dictionary_map_1 = new HashMap<>();
        dictionary_map_1.put("tno", "职工编号");
        dictionary_map_1.put("name", "姓名");
        dictionary_map_1.put("passwd", "初始密码");
        dictionary_map_1.put("sex", "性别");
        dictionary_map_1.put("idcard", "身份证号");
        dictionary_map_1.put("phone", "手机号");
        dictionary_map_1.put("mail", "邮箱");
        dictionary_map_1.put("dept", "所属部门名称");
        List<Map<String, Object>> data_list_1 = new ArrayList<>();
        Map<String, Object> map_1 = new HashMap<>();
        map_1.put("tno", "476323");
        map_1.put("name", "万大力");
        map_1.put("passwd", "123456");
        map_1.put("sex", "男");
        map_1.put("idcard", "620822198702155326");
        map_1.put("phone", "16277115259");
        map_1.put("mail", "123456889@qq.com");
        map_1.put("dept", "体育系");
        data_list_1.add(map_1);
        // 学生信息导入模板格式
        String sheet_name_2 = "Student";
        String title_2 = "学生信息导入模板";
        List<String> list_2 = new ArrayList<>();
        list_2.add("sno"); //学号
        list_2.add("name"); //姓名
        list_2.add("passwd"); //初始密码
        list_2.add("sex"); //性别
        list_2.add("idcard"); //身份证号
        list_2.add("phone"); //手机号
        list_2.add("mail"); //邮箱
        list_2.add("grade"); //入学年份
        list_2.add("dept"); //系部名称
        list_2.add("spec"); //专业名称
        list_2.add("class_no"); //班级编号
        Map<String, String> dictionary_map_2 = new HashMap<>();
        dictionary_map_2.put("sno", "学号");
        dictionary_map_2.put("name", "姓名");
        dictionary_map_2.put("passwd", "初始密码");
        dictionary_map_2.put("sex", "性别");
        dictionary_map_2.put("idcard", "身份证号");
        dictionary_map_2.put("phone", "手机号");
        dictionary_map_2.put("mail", "邮箱");
        dictionary_map_2.put("grade", "入学年份");
        dictionary_map_2.put("dept", "系部名称");
        dictionary_map_2.put("spec", "专业名称");
        dictionary_map_2.put("class_no", "班级编号");
        List<Map<String, Object>> data_list_2 = new ArrayList<>();
        Map<String, Object> map_2 = new HashMap<>();
        map_2.put("sno", "117333440112");
        map_2.put("name", "东方曜");
        map_2.put("passwd", "123456");
        map_2.put("sex", "男");
        map_2.put("idcard", "'450372199902135612");
        map_2.put("phone", "17677157523");
        map_2.put("mail", "98962230@qq.com");
        map_2.put("grade", "2017");
        map_2.put("dept", "计算机科学与工程系");
        map_2.put("spec", "软件工程");
        map_2.put("class_no", "1");
        data_list_2.add(map_2);
        // 使用工具类生成模板文件
        String export_root_path = "C:" + File.separator + "Users" + File.separator + "abc18" + File.separator + "Desktop" + File.separator;
        String file_name = "师生信息导入模板(1).xls";
        // 调用工具生成导入模板
        PersonInfoTemplate.ExportExcelTemplate_XLS_new(template_name,
                sheet_name_1,
                title_1,
                list_1,
                data_list_1,
                dictionary_map_1,
                sheet_name_2,
                title_2,
                list_2,
                data_list_2,
                dictionary_map_2,
                export_root_path + file_name,
                response);

    }

}
